﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Castle.Services.Transaction;
using Meqs.Entity;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement
{
    public class TemplatesLogic : CommonLogic<Templates, int>, ITemplatesLogic
    {
        private readonly IQuestionTypesLogic _qtLogic;
        private readonly IEvalSheetsLogic _sLogic;

        public TemplatesLogic(IQuestionTypesLogic _qtLogic, IEvalSheetsLogic _sLogic)
        {
            this._qtLogic = _qtLogic;
            this._sLogic = _sLogic;
        }

        #region Implementation of ICommonLogic<Templates, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(Templates entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(Templates entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int Delete(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("Templates.DeleteTemplates", ht);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("Templates.DeleteTemplates", ht);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public Templates FindById(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<Templates>("Templates.FindById", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int FindCount(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByCount("Templates.FindCount", ht);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<Templates> FindAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<Templates>("Templates.Find", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<Templates> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        // 删除指定样表内容
        [Transaction(TransactionMode.Requires)]
        public bool DeleteStyleTable(int stId, String schoolMark)
        {
            if (!Deleteable(stId, schoolMark))
                return false;

            Hashtable ht = new Hashtable();
            ht.Add("TemplateID", stId);
            ht.Add("SchoolMark", schoolMark);
            IList<QuestionTypes> reqClsList = _qtLogic.FindAll(ht);
            if (reqClsList != null && reqClsList.Count > 0)
            {
                foreach (QuestionTypes reqClsItem in reqClsList)
                {
                    bool b = _qtLogic.DeleteQuestionsByReqClsId(reqClsItem.RID, schoolMark);
                    if (!b)
                        throw new Exception();
                }
            }

            return this.Delete(stId, schoolMark) > 0 ? true : false;
        }


        /// <summary>
        /// 判断该样表是否正在被引用(如果引用了 就不能被删除)
        /// </summary>
        /// <param name="stId"></param>
        /// <returns></returns>
        private bool Deleteable(int stId, String schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("TemplateID", stId);
            IList<EvalSheets> siList = _sLogic.FindAll(ht);
            if (siList.Count > 0)
                return false;

            return true;
        }

        #region ITemplatesLogic 成员


        // 复制选中的样表并新建一个样表
        [Transaction(TransactionMode.RequiresNew)]
        public int CopyStyleTableToNewtable(int destClsId, int srcStId, String destStName, String schoolMark, int style)
        {
            int newTableId = 0;
            Templates st = this.FindById(srcStId, schoolMark);
            Templates newTable = new Templates();
            newTable.EvalItemID = destClsId;
            newTable.TemplateName = destStName;
            newTable.TemplateStyle = (TemplateStyle)style;
            newTable.EvalType = st.EvalType;
            newTableId = this.CreateTemplates(newTable, schoolMark);
            //newTableId = this.GetIdentity();
            if (newTableId < 1)
                throw new Exception();

            Hashtable ht = new Hashtable();
            ht.Add("TemplateID", srcStId);
            ht.Add("SchoolMark", schoolMark);
            IList<QuestionTypes> reqClsList = _qtLogic.FindAll(ht);
            foreach (QuestionTypes reqClsItem in reqClsList)
            {
                bool b = _qtLogic.CopyReqClsToNewTable(newTableId, reqClsItem.RID, schoolMark);
                if (!b)
                    throw new Exception();
            }

            return newTableId;
        }


        /// <summary>
        /// 添加样表
        /// </summary>
        public int CreateTemplates(Templates template, String schoolMark)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("Templates", template);
                ht.Add("SchoolMark", schoolMark);

                int id = (int)InsertCustomer("Templates.InsertTemplatesEx", ht);
                return id;
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        /// <summary>
        /// 更新样表
        /// </summary>
        public int UpdateTemplates(Templates template, String schoolMark)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("Templates", template);
                ht.Add("SchoolMark", schoolMark);

                int id = (int)UpdateCustomer("Templates.UpdateTemplatesEx", ht);
                return id;
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        /// <summary>
        /// 获取项目下的样表
        /// </summary>
        /// <param name="itemsID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<Templates> FindTemplateByEvalItems(int itemsID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("EvalItemID", itemsID);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<Templates>("Templates.FindTemplateByEvalItems", ht);
        }

        public int GetTemplateScore(String schoolMark, int tID)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SeID", tID);
            ht.Add("SchoolMark", schoolMark);
            try
            {
                IList<int> list = FindCustomer<int>("Templates.GetTemplateScore", ht);
                if (list != null && list.Count > 0)
                    return list[0];
            }
            catch (Exception e)
            {
                return 0;
            }
            return 0;
        }

        #endregion
    }
}
